package com.sicau.controller;


import com.sicau.constants.CommonConstants;
import com.sicau.entity.dto.Composer;
import com.sicau.entity.pojo.vo.ResultVO;
import com.sicau.service.ComposerService;
import com.sicau.utils.JWT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@RestController
public class ComposerController {

    @Autowired
    ComposerService composerService;


    @RequestMapping(CommonConstants.PUB_PREFIX+"/user/login")
    public ResultVO login(HttpServletResponse response,Composer composer){

        response.addHeader("Access-Control-Allow-Methods","*");
        response.addHeader("Access-Control-Allow-Origin","*");
//        response.addHeader("Access-Control-Max-Age","100");
        response.addHeader("Access-Control-Allow-Headers", "Content-Type,token,Cookie");
//        //允许客户端发送cookies true表示接收，false不接受 默认为false？
        response.addHeader("Access-Control-Allow-Credentials","true");
        response.setHeader("token", JWT.sign(composer, 1));
        response.setHeader("Access-Control-Expose-Headers", "token");

        return composerService.login(composer);


    }

    @RequestMapping(CommonConstants.NONPUBLIC_PREFIX+"/user/notice")
    public ResultVO getNotice(HttpServletResponse response,String id,String location){
        return composerService.getNotice(id,location);
    }

    @RequestMapping(CommonConstants.NONPUBLIC_PREFIX+"/user/personalInfo")
    public ResultVO getPersonalInfo(HttpServletResponse response,String id){
        return composerService.getPersonalInfo(id);
    }


    @RequestMapping(CommonConstants.NONPUBLIC_PREFIX+"/user/changePassword")
    public ResultVO updatePassword(HttpServletResponse response,Composer composer,String oldPassword,String newPassword){
        composer.setPassword(oldPassword);
        return composerService.updatePassword(composer,newPassword);
    }

    @RequestMapping(CommonConstants.NONPUBLIC_PREFIX+"/user/index")
    public ResultVO updatePassword(Composer composer){
        return composerService.getIndexInfo(composer);
    }







}
